<template>
  <div class="common-layout">
    <el-container>
      <el-header class="header-wrapped">
        <div class="header-content">
          <h3>豆瓣数据可视化系统</h3>
          <span class="welcome">欢迎您的登录</span>
        </div>
      </el-header>
      <el-main>
        <div class="login-wrapped">
          <el-card class="box-card">
            <el-tabs v-model="activeName" class="demo-tabs" :stretch="true">
              <el-tab-pane label="登录" name="first">
                <el-form
                  class="login-form"
                  :model="loginData"
                  :rules="loginRules"
                >
                  <el-form-item label="账号">
                    <el-input
                      v-model="loginData.account"
                      placeholder="请输入账号"
                    />
                  </el-form-item>
                  <el-form-item label="密码">
                    <el-input
                      v-model="loginData.password"
                      placeholder="请输入密码"
                      show-password
                    />
                  </el-form-item>
                  <!-- 底部外壳 -->
                  <div class="footer-wrapped">
                    <div class="forget-password">
                      <span class="forget-password-button" @click="openForget"
                        >忘记密码</span
                      >
                    </div>
                    <div class="footer-button">
                      <el-button type="primary" @click="Login">登录</el-button>
                    </div>
                    <div class="footer-go-register">
                      还没有账号？<span class="go-register">马上去注册</span>
                    </div>
                  </div>
                </el-form>
              </el-tab-pane>
              <el-tab-pane label="注册" name="second">
                <el-form
                  class="login-form"
                  ref="registerDataref"
                  :model="registerData"
                  :rules="registerRules"
                >
                  <el-form-item label="账号" prop="account">
                    <el-input
                      v-model="registerData.account"
                      placeholder="账号长度6-12位"
                    />
                  </el-form-item>
                  <el-form-item label="密码" prop="password">
                    <el-input
                      v-model="registerData.password"
                      placeholder="密码需长度6-12位含字母数字"
                    />
                  </el-form-item>
                  <el-form-item label="确认密码" prop="nextPassword">
                    <el-input
                      v-model="registerData.nextPassword"
                      placeholder="请再次输入密码"
                    />
                  </el-form-item>
                  <div class="footer-button">
                    <el-button type="primary" @click="Register(registerDataref)"
                      >注册</el-button
                    >
                  </div>
                </el-form>
              </el-tab-pane>
            </el-tabs>
          </el-card>
        </div>
      </el-main>
      <el-footer class="footer-wrapped">
        <div class="footer-content">
          <div class="title">
            <span>坚持“富矿精开”战略</span> |
            <span>推动产业升级与产能优化</span> |
            <span>强化科技创新与人才培养</span> |
            <span>延长产业链，提升附加值</span> |
            <span>积极履行社会责任</span> |
            <span>未来目标与愿景</span>
          </div>
        </div>
      </el-footer>
    </el-container>
  </div>
  <!-- <forget ref="forgetP"></forget> -->
</template>

<script lang="ts" setup>
import { ref, reactive } from "vue";
import { ElMessage, FormInstance, FormRules } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();
// 表单接口
interface FormData {
  account: number | null;
  password: string;
  nextPassword?: string;
}
// 登录表单数据
const loginData: FormData = reactive({
  account: null,
  password: "",
});
// 注册表单数据
const registerData: FormData = reactive({
  account: null,
  password: "",
  nextPassword: "",
});

const loginDataref = ref<FormInstance>();
const registerDataref = ref<FormInstance>();
// //表单验证
// const loginRules = reactive<FormRules<loginData>>({
//   account: [
//     { required: true, message: "请输入账号", trigger: "blur" },
//     { min: 6, max: 12, message: "账号是6-12位的数字", trigger: "blur" },
//   ],
//   password: [
//     {
//       required: true,
//       message: "请输入密码",
//       trigger: "blur",
//     },
//     {
//       min: 6,
//       max: 12,
//       message: "密码是6-12的数字",
//       trigger: "blur",
//     },
//     {
//       validator: (rule, value, callback) => {
//         const regex = /^(?![0-9]+$)[a-z0-9]{1,50}$/;
//         if (!regex.test(value)) {
//           callback(
//             new Error("密码必须包含至少一个字母和一个数字，且不能全是数字")
//           );
//         } else {
//           callback();
//         }
//       },
//       trigger: "blur",
//     },
//   ],
// });

// 注册验证
const registerRules = reactive<FormRules<registerData>>({
  account: [
    { required: true, message: "请输入注册账号", trigger: "blur" },
    { min: 6, max: 12, message: "账号是6-12位的数字", trigger: "blur" },
  ],
  password: [
    {
      required: true,
      message: "请输入密码",
      trigger: "blur",
    },
    {
      min: 6,
      max: 12,
      message: "密码是6-12的数字",
      trigger: "blur",
    },
    {
      validator: (rule, value, callback) => {
        const regex = /^(?![0-9]+$)[a-z0-9]{1,50}$/;
        if (!regex.test(value)) {
          callback(
            new Error("密码必须包含至少一个字母和一个数字，且不能全是数字")
          );
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
  nextPassword: [
    {
      required: true,
      message: "请再次输入密码",
      trigger: "blur",
    },
    {
      min: 6,
      max: 12,
      message: "密码是6-12的数字",
      trigger: "blur",
    },
    {
      validator: (rule, value, callback) => {
        const regex = /^(?![0-9]+$)[a-z0-9]{6,12}$/;
        if (!regex.test(value)) {
          callback(
            new Error("密码必须包含至少一个字母和一个数字，且不能全是数字")
          );
        } else if (value !== registerData.password) {
          callback(new Error("两次输入的密码不一致"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
});

// 登录
const Login = async (formEl: FormInstance | undefined) => {
  // if (!formEl) return;
  // await formEl.validate(async (valid, fields) => {
  //   if (valid) {
  //     const res = await login(loginData);
  //     if (res.message == "登录成功") {
  //       const { id, name, account, email, department } = res.results;
  //       const { token } = res;
  //       ElMessage({
  //         message: "登录成功",
  //         type: "success",
  //       });
  //       localStorage.setItem("id", id);
  //       localStorage.setItem("token", token);
  //       localStorage.setItem("name", name);
  //       localStorage.setItem("department", department);
  //       await loginLog(account, name, email);
  //       store.userInfo(id);
  //       // 跳转
  //       router.push("/home");
  //     } else {
  //       ElMessage.error(res.message);
  //     }
  //   } else {
  //     console.log("error submit!", fields);
  //   }
  // });
  if (loginData.account === "admin" && loginData.password === "123456") {
    // 可以跳转到主页或其他页面
    // window.location.href = '/home';
    // 跳转
    router.push("/home");
  } else {
    // 登录失败，显示错误信息
    document.getElementById("message").textContent =
      "Invalid username or password.";
  }
};

// 注册

const Register = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  // await formEl.validate(async (valid, fields) => {
  //   if (valid) {
  //     const res = await register(registerData);
  //     if (res.message == "注册账号成功") {
  //       ElMessage({
  //         message: "注册成功",
  //         type: "success",
  //       });
  //       activeName.value = "first";
  //     } else {
  //       ElMessage.error("注册失败，请检查数据是否正确");
  //     }
  //   } else {
  //     ElMessage.error("注册失败");
  //   }
  // });
};

// 忘记密码弹窗
// const forgetP = ref();
// // 打开忘记密码弹窗
// const openForget = () => {
//   forgetP.value.open();
// };
</script>

<style lang="scss" scoped>
// 头部外壳
.header-wrapped {
  // 头部内容
  .header-content {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;

    // 欢迎语
    .welcome {
      font-size: 13px;
    }
  }
}

// 主体部分
.el-main {
  background-image: url("@/assets/bg.jpg");
  background-size: cover;
  height: 600px;
  --el-main-padding: 0;

  // 登录外壳
  .login-wrapped {
    width: 1200px;
    height: 600px;
    margin: 0 auto;

    // 卡片样式
    .box-card {
      width: 350px;
      height: 375px;
      float: right;
      position: relative;
      top: 14%;
      opacity: 0.9;
      border-radius: 20px;
      .login-form {
        // 登录底部外壳
        .footer-wrapped {
          display: flex;
          flex-direction: column;

          .forget-password {
            display: flex;
            justify-content: flex-end;
            margin: 10px 0;

            .forget-password-button {
              font-size: 12px;
              color: #409eff;
              // 鼠标移入的效果
              cursor: pointer;
            }
          }

          .footer-go-register {
            font-size: 12px;
            margin: 12px 0;
            display: flex;
            justify-content: center;

            .go-register {
              font-size: 12px;
              cursor: pointer;
            }
          }
        }

        // 底部登录按钮
        .footer-button {
          width: 100%;
          display: flex;
          justify-content: center;
        }
      }
    }
  }
}

// 底部外壳
.footer-wrapped {
  margin-top: 10px;

  // 底部内容
  .footer-content {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;

    // 底部介绍
    .title {
      color: #666;
    }

    span {
      color: #666;
      font-size: 12px;
    }
  }
}

// 表单边距
.el-form {
  margin-top: 30px;
}

// tabs标签
:deep(.el-tabs__item) {
  color: #333;
  font-size: 18px;
}

// 输入框高度
:deep(.el-input__inner) {
  height: 40px;
}

// 输入框标签字体高度
:deep(.el-form-item__label) {
  height: 40px;
  line-height: 40px;
}

// 登录按钮
:deep(.el-button) {
  width: 300px;
  height: 45px;
  font-size: 16px;
}
</style>
